﻿package cgs.server.logging.actions
{
    import flash.events.*;
    import flash.utils.*;

    public class DefaultActionBufferHandler extends Object implements IActionBufferHandler
    {
        private var _timer:Timer;
        private var _listener:IActionBufferListener;
        private var _elapsedTime:Number;
        private var _minTime:Number;
        private var _maxTime:Number;
        private var _rampTime:Number;

        public function DefaultActionBufferHandler()
        {
            this._elapsedTime = 0;
            return;
        }// end function

        public function setProperties(param1:Number, param2:Number, param3:Number) : void
        {
            this._minTime = param1;
            this._maxTime = param2;
            this._rampTime = param3;
            return;
        }// end function

        private function handleTimer(event:TimerEvent) : void
        {
            if (this._listener != null)
            {
                this._listener.flushActions();
            }
            if (this._elapsedTime < this._rampTime)
            {
                this._elapsedTime = this._elapsedTime + this._timer.delay;
                if (this._elapsedTime > this._rampTime)
                {
                    this._elapsedTime = this._rampTime;
                }
            }
            this._timer.reset();
            this._timer.delay = this.getNextFlushTime();
            this._timer.start();
            return;
        }// end function

        private function getNextFlushTime() : Number
        {
            if (this._rampTime == 0)
            {
                return this._maxTime;
            }
            return this._elapsedTime / this._rampTime * (this._maxTime - this._minTime) + this._minTime;
        }// end function

        public function set listener(param1:IActionBufferListener) : void
        {
            this._listener = param1;
            return;
        }// end function

        public function start() : void
        {
            if (this._timer == null)
            {
                this._timer = new Timer(this.getNextFlushTime(), 1);
                this._timer.addEventListener(TimerEvent.TIMER_COMPLETE, this.handleTimer);
            }
            this._timer.start();
            return;
        }// end function

        public function stop() : void
        {
            if (this._timer != null)
            {
                this._timer.stop();
            }
            return;
        }// end function

        public function reset() : void
        {
            this._elapsedTime = 0;
            if (this._timer != null)
            {
                this._timer.reset();
                this._timer.delay = this.getNextFlushTime();
            }
            return;
        }// end function

        public function onTick(param1:Number) : void
        {
            return;
        }// end function

    }
}
